export * from './composables'

import { fetchEventSource, type FetchEventSourceInit } from '@microsoft/fetch-event-source'
import { LocalStorage } from '@/constants/localStorage'
import { fetcher } from '../fetcher'
import type { GetReportData, SearchReportData, SearchReportQueryKey } from './types'

export function subscribeReport(reportID: number, options: FetchEventSourceInit) {
  const abortController = new AbortController()

  fetchEventSource(`${import.meta.env.VITE_API_BASE_URL}/reports/${reportID}/subscribe`, {
    method: 'GET',
    mode: 'cors',
    headers: {
      Authorization: `Bearer ${localStorage.getItem(LocalStorage.Token)}`
    },
    signal: abortController.signal,
    ...options
  })

  return abortController
}

export function ApiGetReport(id: number) {
  return fetcher<GetReportData>(`/reports/${id}`)
}

export function ApiSearchReport(query: SearchReportQueryKey[2]) {
  return fetcher<SearchReportData>('/reports', { query })
}
